package com.hou.hpsj_springboot.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.jwt.JWT;
import com.hou.hpsj_springboot.bean.Users;
import com.hou.hpsj_springboot.entity.R;
import com.hou.hpsj_springboot.service.LoginService;
import com.hou.hpsj_springboot.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
@Slf4j
@RestController
@RequestMapping("login")
public class LoginController {
    @Resource
    private LoginService LoginService;
    /**
     * 获取验证码
     */
    @GetMapping("captcha")
    public void getCaptcha(String email) throws MessagingException, UnsupportedEncodingException {
        LoginService.getCaptcha(email);
    }
    /**
     * 登录注册
     * @param user
     * @return
     * @throws MessagingException
     * @throws UnsupportedEncodingException
     */
    @PostMapping("registerOrLogin")
    public R registerOrLogin(@RequestBody Users user)  {
        try {
            log.info("正在注册或登录{}...", user);
            LoginService.registerOrLogin(user);
            return R.ok("登录成功");
        } catch (Exception e) {
            log.error("登录失败:{}", e.getMessage());
            throw new RuntimeException(e);
        }

    }
}
